<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<script type="text/javascript" >
var userSelectTree = ctx + '/sm/User!findUserTree.action';
var callAll = false;
var selectNodeAct;
$(function(){
	$('#selectCombo').combobox({
        url: ctx+'/wfc/Task!findNextNode.action?entity.sid='+$('#select_task_id').val(),
        valueField: 'sid',
        textField: 'name',
        onSelect: function(o){
        	selectNodeAct = o;
        	if(o.clazz=='E'){
        		$("#taskOperation","#selectTaskWindows").children().remove();
            	$("#taskOperation","#selectTaskWindows").append("<td colspan=\"3\"><div id=\"endTable\" style=\"text-align:center;align:center\" align=\"center\">流程结束</div></td>");
            }else{
        		targetCompanyTree();
        	}
        },
		onLoadSuccess: function(){
			if (null == $('#selectCombo').combobox('getData')[0]) {
				
			}else{
				var firstValue = $('#selectCombo').combobox('getData')[0];
				$('#selectCombo').combobox('select', firstValue.sid);
			}
		}
	});
	
});


function targetCompanyTree(){
	$('#target_task_tree').tree({
		url: userSelectTree,
        fit: true,
        idField: 'id',
        treeField: 'text',
        onBeforeLoad : function(row, param) {
			if (row) {
				userSelectTree = ctx + '/sm/User!findUserTree.action?id='+row.id;
			}
		},
		onDblClick : function(node){
			if ($('#target_task_tree').tree("isLeaf", node.target)) {
				addI(node);
			}
		},onClick : function(node) {
			callAll = false;
		},onExpand : function(node) {
			if (callAll) {
					var nodeSelect = $('#target_task_tree').tree("getSelected");
					if (nodeSelect == null)
										return;
					if (node.id == nodeSelect.id|| isParent(node, nodeSelect)) {
							addItemNodes(node);
					} else {
						callAll = false;
					}
			}
		},onLoadSuccess: function(){
			$('#selectloadMsg').hide();
		}
	});
};


$('#selectClosed','#selectTaskWindows').bind('click',function(){
	showHideFun('taskPopup','close');
});

$('#selectConform','#selectTaskWindows').bind('click',function(){
	var rows = [];
	if(selectNodeAct.clazz!='E'){
		//获取选择的操作对象列表
		var opts = document.getElementById("selectedTargets").options;
		if (opts.length == 0) {
	        $.messager.show({
	            msg : "请选择人员!"
	        });
	        return false;
	    }
		if('0'==selectNodeAct.isMultiTask){
			if (opts.length != 1) {
	            $.messager.show({
	                msg : "请只选择一个处理人员!"
	            });
	            return false;
	        }
		}
		
		for ( var i = 0; i < opts.length; i++) {
			var row = {};
			row.id = opts[i].value;
			row.text = opts[i].text;
			row.mobile = $(opts[i]).attr('mobile');
			row.email = $(opts[i]).attr('email');
			rows.push(row);
		}
	}
	CQOA.completeTaskCallback(rows,$('#OperationTypes').val(),selectNodeAct.sid);
	showHideFun('taskPopup','close');
});



//添加本级节点
function addItem() {
	var node = $('#target_task_tree').tree("getSelected");//选择的节点
	if (node == null)
		return;//如果为NULL，则返回
	if ($('#target_task_tree').tree("isLeaf", node.target)) {
			addI(node);
	}
};
//添加
function addI(node) {
	if (existItem(node.id)) {
		return;//如果已经存在，则返回
	}
	var opt = "<option id='option" + node.id + "' value='" + node.id + "' email='"+node.attributes.email+"' mobile='"+node.attributes.mobile+"'>" + node.text + "</option>";
	$(opt).appendTo($("#selectedTargets"));
	
}
//是否是他的父节点
function isParent(node,select) {
	var thisNode = $('#target_task_tree').tree("getParent", node.target);
	if (thisNode.id == select.id) {
		return true;
	} else {
		var root = $('#target_task_tree').tree("getRoot");
		if (thisNode.id == root.id) {
			return false;
		} else {
			return isParent(thisNode, select);
		}
	}
};

//添加所有节点
function addAllItem() {
	var node = $('#target_task_tree').tree("getSelected");
	if (node == null)
		return;
	if ($('#target_task_tree').tree("isLeaf", node.target)) {
		addI(node);//添加本结点
	}
	callAll = true;
	$('#target_task_tree').tree("expandAll", node.target);
	addItemNodes(node);
}
function addItemNodes(node) {
	//添加子结点
	var nodes = $('#target_task_tree').tree("getChildren", node.target);
	for ( var i = 0; i < nodes.length; i++) {
			var child = nodes[i];
			//全单位的时候点击全选择递归查询
			if(callAll){
				$('#target_task_tree').tree("expandAll", child.target);
			}
			if ($('#target_task_tree').tree("isLeaf", child.target)) {
				addI(child);
			}
	}
}
//移除指定的
function removeItem() {
	var lst = document.getElementById("selectedTargets");
	var idx = lst.selectedIndex;
	if (idx >= 0)
		lst.remove(idx);
	if (idx < lst.options.length)
		lst.selectedIndex = idx;
	else if (lst.options.length > 0)
		lst.selectedIndex = lst.options.length - 1;
}
//清空
function clearItem() {
	document.getElementById("selectedTargets").options.length = 0;
}
//判断是否存在
function existItem(key) {
	var result = false;
	$("#selectedTargets option").each(function() {
		if ($(this).val() == key) {
			result = true;
			return result;
		}
	});
	return result;
}


</script>
<div id="selectTaskWindows" style="width: 100%; height: 400px;">
	<s:hidden id="select_task_id" name="entity.sid"></s:hidden>
	<table style="height: 100%;width: 100%;">
	    <tr style="height: 30px;text-align: center;">
			<td colspan="3">
				<div id="selectNodes">
					<select id="selectCombo"  class="easyui-combobox" style="width: 200px;margin-top: 3px;margin-left: 3px;"/>
				</div>
			</td>
		</tr>
		<tr id="taskOperation">
			<td style="width: 45%;">
				<!-- 选择的类型  1 人员  2部门  3岗位 -->
				<s:hidden name="selectTypes" value="1" id="OperationTypes"></s:hidden>
				<div style="width: 100%; height: 350px; overflow: auto;border: 1px solid;">
							<ul id="target_task_tree" class="easyui-tree"></ul>
							<div id="selectloadMsg">数据加载中,请稍候...</div>
				</div>
			</td>
			<td style="width: 5%;">
					<p><input type="image" src="<s:url value='/images/icon/a_select.jpg'/>" style="border: 1px solid;" title="选择本项" onClick="addItem();"></p>
					<p><input type="image" src="<s:url value='/images/icon/all_select.jpg'/>"  style="border: 1px solid;" title="选择本项以下所有" onClick="addAllItem();"></p>
					<p><input type="image" src="<s:url value='/images/icon/un_select.jpg'/>" style="border: 1px solid;" title="取消选择" onClick="removeItem();"></p>
					<p><input type="image" src="<s:url value='/images/icon/unAll_select.jpg'/>" style="border: 1px solid;" title="取消所有" onClick="clearItem();"></p>
			</td>
			<td style="width: 45%;">
				<select id="selectedTargets" name="select" size="2" style="width: 100%; height: 350px;" >
				</select>
			</td>
		</tr>
	</table>
	<div  border="false" style="height: 40px; text-align: center; width: 100%;padding-top: 5px;">
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok" id="selectConform">确定</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" id="selectClosed">取消</a>
	</div>
</div>